package com.coderg.gmall.product.controller;

import com.alibaba.fastjson.JSONObject;
import com.coderg.gmall.common.cache.MyGmallCache;
import com.coderg.gmall.common.constant.RedisConst;
import com.coderg.gmall.model.product.*;
import com.coderg.gmall.product.service.ItemService;
import com.coderg.gmall.product.service.RedisTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @Description 商品详情微服务 内部调用controller
 * @Author CoderG
 * @Date 2025/5/16 18:05
 **/
@RestController
@RequestMapping(value = "/api/item")
public class ItemController {

    @Autowired
    ItemService itemService;


    /**
     * 根据skuId查询商品的平台属性
     *
     * @param skuId
     * @return
     */
    @GetMapping("/getAttrList/{skuId}")
    public List<BaseAttrInfo> getAttrList(@PathVariable("skuId") Long skuId){
       return itemService.getAttrList(skuId);

    }
    /**
     * 获取品牌详情B
     *
     * @param tmId
     * @return
     */
    @GetMapping("/getTrademark/{tmId}")
    public BaseTrademark getTrademarkByTmId(@PathVariable("tmId") Long tmId){
        return itemService.getBaseTrademark(tmId);
    }

    /**
     * 获取sku详情
     *
     * @param skuId
     * @return
     */
    @MyGmallCache(prefix = RedisConst.SKUKEY_PREFIX)
    @GetMapping(value = "/getSkuInfo/{skuId}")
    public SkuInfo getSkuInfo(@PathVariable("skuId") Long skuId) {
        return itemService.getSkuInfo(skuId);
    }

    /**
     * 获取分类视图
     *
     * @param category3Id
     * @return
     */
    @MyGmallCache(prefix = "category3Id:")
    @GetMapping(value = "/getBaseCategory/{category3Id}")
    public BaseCategoryView getBaseCategory(@PathVariable("category3Id") Long category3Id) {
        return itemService.getBaseCategory(category3Id);
    }

    /**
     * 获取价格
     *
     * @param skuId
     * @return
     */
    @MyGmallCache(prefix = "price:")
    @GetMapping(value = "/getSkuPrice/{skuId}")
    BigDecimal getSkuPrice(@PathVariable("skuId") Long skuId) {
        return itemService.getSkuPrice(skuId);
    }

    /**
     * 通过skuId,spuId查询 销售属性和值以及标记当前选中的销售属性值
     *
     * @param spuId
     * @param skuId
     * @return
     */
    @MyGmallCache(prefix = "spuIdAndSkuId:")
    @GetMapping(value = "/getSpuAttrValueBy/{spuId}/{skuId}")
    List<SpuSaleAttr> getSpuAttrValueBy(@PathVariable("spuId") Long spuId,
                                        @PathVariable("skuId") Long skuId) {
        return itemService.getSpuAttrValueBy(spuId, skuId);
    }

    /**
     * 获取销售属性 和具体商品sku值的对应关系 键值对关系返回  sku-->sale_attr_value_id
     *
     * @param spuId
     * @return
     */
    @MyGmallCache(prefix = "spuIdAndSaleAttrValueId:")
    @GetMapping(value = "/getSkuAndSaleBySpuId/{spuId}")
    public Map getSkuAndSaleBySpuId(@PathVariable("spuId") Long spuId) {
        return itemService.getSkuAndSaleBySpuId(spuId);
    }


    /**
     * 获取首页所有目录集合 生成静态页面时使用
     *
     * @return
     */
    @GetMapping(value = "/getFirstCategory123")
    public List<JSONObject> getFirstCategory123() {
        return itemService.getFirstCategory123();
    }


    @Autowired
    RedisTestService redisTestService;

    /**
     * 查询sku详情 通过缓存  （无用接口）
     *
     * @param skuId
     * @return
     */
    @GetMapping(value = "/getSkuInfoForRedisOrMysql/{skuId}")
    public SkuInfo getSkuInfoForRedisOrMysql(@PathVariable("skuId") Long skuId) {
        return redisTestService.getSkuInfo1(skuId);
    }

    /**
     * 查询sku详情 通过缓存  （无用接口）
     *
     * @param skuId
     * @return
     */
    @GetMapping(value = "/getSkuInfoForRedis/{skuId}")
    public SkuInfo getSkuInfoForRedis(@PathVariable("skuId") Long skuId) {
        return redisTestService.getSkuInfo2(skuId);
    }


    /**
     * 扣减库存
     * @param stockpile
     * @return
     */
    @PostMapping("/decreaseStockpile")
    Boolean decreaseStockpile(@RequestBody Map<Long,Integer> stockpile){
        try {
           return itemService.decreaseStockpile(stockpile);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    /**
     * 回退库存
     * @param stockMap
     * @return
     */
    @GetMapping("/rollbackStock")
    boolean rollbackStock(@RequestParam Map<String, String> stockMap){
        try {
            return itemService.rollbackStock(stockMap);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
